
#include "lin_space_tiling.h"
#include "register/op_def_registry.h"
#include <algorithm>
#include "tiling/platform/platform_ascendc.h"

namespace optiling {
    const uint32_t BLOCK_SIZE = 32;
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

    LinSpaceTilingData tiling;
    uint64_t ubSize;
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    // auto socVersion = ascendcPlatform.GetSocVersion();
    ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize); //获取硬件平台存储空间 UB 的内存大小
    // auto aivNum = ascendcPlatform.GetCoreNum(); //获取当前硬件平台的核数 此平台为1


    //获取输入shape信息
    uint32_t inputNum = context->GetInputShape(0)->GetStorageShape().GetShapeSize(); //输入数量
    uint32_t inputBytes = GetSizeByDataType(context->GetInputDesc(0)->GetDataType()); //输入类型
    //get datatype
    int dtype = context->GetInputDesc(0)->GetDataType();

    uint32_t inputLength = inputBytes * inputNum;
    uint32_t ubDataNumber = 10;
    if(inputBytes == 2) {
        ubDataNumber = 10;
    } else if(inputBytes == 4) {
        ubDataNumber = 6;
    } 
    
    
    if(dtype == ge::DT_BF16) {
        std::cout << "bf16" << std::endl;
        context->SetTilingKey(100);
        ubDataNumber = 20;
    } else if(dtype == ge::DT_INT8) {
        std::cout << dtype << "int8" <<std::endl;
        context->SetTilingKey(200);
    } else if(dtype == ge::DT_INT32) {
        std::cout << dtype << "int32" <<std::endl;
        context->SetTilingKey(201);
    } else if(dtype == ge::DT_UINT8) {
        std::cout << dtype << "uint8" <<std::endl;
        context->SetTilingKey(202);    
        // const gert::Tensor *start_tensor = context->GetInputTensor(0);
        // if (start_tensor == nullptr) {
        //     return ge::GRAPH_FAILED;
        // }
        // auto numL = start_tensor->GetData<uint8_t>();
        // int32_t num = *numL;    

    } else if(dtype == ge::DT_INT16) {
        std::cout << dtype << "int16" <<std::endl;
        context->SetTilingKey(203);            
    } else if(dtype == ge::DT_FLOAT16) {
        std::cout << "half" << std::endl;
        context->SetTilingKey(300);
        ubDataNumber = 20;
    } else {
        std::cout << "float32" << std::endl;
        context->SetTilingKey(400);
        ubDataNumber = 20;
    }

    std::cout << "inputBytes:"<< inputBytes << std::endl;
    // The number of 32B data blocks that can be used for each data. DOUBLE BUFFER is already counted here
    uint32_t tileBlockNum = (ubSize / BLOCK_SIZE) / ubDataNumber; //每个ub段可用的空间块数
    uint32_t tileDataNum = (tileBlockNum * BLOCK_SIZE) / inputBytes; //每次处理的数据量

    // Input data for 32B alignment
    uint32_t inputLengthAlgin32 = (((inputLength + BLOCK_SIZE - 1) / BLOCK_SIZE) * BLOCK_SIZE); //输入长度 对齐处理
    // There is at least 32B of data on each core, satisfying several settings for several cores. The maximum number of audits is the actual number of audits
    uint32_t everyCoreInputBlockNum = inputLengthAlgin32 / BLOCK_SIZE;// 输入数据需要多少空间块
    // uint32_t tailBlockNum = (inputLengthAlgin32 / BLOCK_SIZE) % aivNum;
    
    //  chunks are calculated and sliced several times using the number of data on each core
    uint32_t CoreDataNum = everyCoreInputBlockNum * BLOCK_SIZE / inputBytes; //对齐空间后的输入数量
    uint32_t TileNum = everyCoreInputBlockNum / tileBlockNum;
    uint32_t finalTileNum = (everyCoreInputBlockNum % tileBlockNum) == 0 ? TileNum : TileNum + 1; //需要循环处理几次
    // Tail block calculation for  chunks of data
    uint32_t TailDataNum = CoreDataNum - (tileDataNum * TileNum);
    TailDataNum = TailDataNum == 0 ? tileDataNum : TailDataNum; //最后一次需要处理的数据量

    const gert::Tensor *num_tensor = context->GetInputTensor(2);
    if (num_tensor == nullptr) {
        return ge::GRAPH_FAILED;
    }
    auto numL = num_tensor->GetData<int32_t>();
    int32_t num = *numL;    
    std::cout << "num:" << num << std::endl;
    if(num == 1) {
        context->SetTilingKey(900);
    }

    tiling.set_CoreDataNum(CoreDataNum);  //对齐空间后的输入数量
    tiling.set_finalTileNum(finalTileNum);//需要循环处理几次
    tiling.set_tileDataNum(tileDataNum); //每次处理的数据量
    tiling.set_TailDataNum(TailDataNum); //最后一次需要处理的数据量
    std::cout << "tiling_key:" << context->GetTilingKey() << std::endl;
    std::cout << "CoreDataNum:" << CoreDataNum << std::endl;
    std::cout << "finalTileNum:" << finalTileNum << std::endl;
    std::cout << "tileDataNum:" << tileDataNum << std::endl;
    std::cout << "TailDataNum:" << TailDataNum << std::endl;

    context->SetBlockDim(1);
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

    return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class LinSpace : public OpDef {
public:
    explicit LinSpace(const char* name) : OpDef(name)
    {
        this->Input("start")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_INT8, ge::DT_UINT8, ge::DT_INT32, ge::DT_INT16, ge::DT_FLOAT16, ge::DT_BF16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("stop")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_INT8, ge::DT_UINT8, ge::DT_INT32, ge::DT_INT16, ge::DT_FLOAT16, ge::DT_BF16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("num_axes")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("output")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_INT8, ge::DT_UINT8, ge::DT_INT32, ge::DT_INT16, ge::DT_FLOAT16, ge::DT_BF16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(LinSpace);
}
